package bb.lanxing.util;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import bb.lanxing.App;
import bb.lanxing.common.config.Constants;
import bb.lanxing.manager.SharedManager;

public class ImageUtil {
    public static File buildTakePhotoDir() {
        File file;
        String buildExternalDirectoryPath = AppFileUtils.buildExternalDirectoryPath(Constants.PHOTO);
        if (buildExternalDirectoryPath != null) {
            file = new File(buildExternalDirectoryPath);
        } else {
            File externalCacheDir = App.getContext().getExternalCacheDir();
            if (externalCacheDir == null) {
                return null;
            }
            file = new File(externalCacheDir, Constants.PHOTO);
        }
        try {
            if (file.exists()) {
                file.mkdirs();
            }
            AppFileUtils.createNomediaFile(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return file;
    }

    public static String buildTopicImagePath() {
        int userId = SharedManager.getInstance().getUserId();
        return "topic" + File.separator + userId + File.separator + System.currentTimeMillis() + ".jpg";
    }

    public static String buildTopicCommentImagePath() {
        int userId = SharedManager.getInstance().getUserId();
        return "topic_comment" + File.separator + userId + File.separator + System.currentTimeMillis() + ".jpg";
    }

    public static String buildBikePlaceCommentImagePath() {
        int userId = SharedManager.getInstance().getUserId();
        return "bike" + File.separator + userId + File.separator + System.currentTimeMillis() + ".jpg";
    }

    public static String buildClubcImagePath() {
        int userId = SharedManager.getInstance().getUserId();
        return "club" + File.separator + userId + File.separator + System.currentTimeMillis() + ".jpg";
    }

    public static String buildLushuCommentImagePath() {
        int userId = SharedManager.getInstance().getUserId();
        return "lushuComment" + File.separator + userId + File.separator + System.currentTimeMillis() + ".jpg";
    }

    public static String buildLushuWaypointImagePath() {
        int userId = SharedManager.getInstance().getUserId();
        return "lushuWaypoint" + File.separator + userId + File.separator + System.currentTimeMillis() + ".jpg";
    }

    public static void storeJPGImage(Bitmap bitmap, String str, int i) {
        File file = new File(str);
        if (!file.getParentFile().exists()) {
            file.getParentFile().mkdirs();
        }
        try {
            FileOutputStream fileOutputStream = new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.JPEG, i, fileOutputStream);
            BitmapHelper.recycleBitmap(bitmap);
            fileOutputStream.flush();
            fileOutputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static Bitmap getCanUploadSourceImage(String str) {
        return getCanUploadSourceImage(str, false);
    }

    public static Bitmap getCanUploadSourceImage(String str, boolean z) {
        int imageDegree;
        BitmapFactory.Options options = new BitmapFactory.Options();
        int i = 1;
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(str, options);
        options.inSampleSize = i;
        options.inJustDecodeBounds = false;
        Bitmap decodeFile = BitmapFactory.decodeFile(str, options);
        if (!z || (imageDegree = CommonUtil.getImageDegree(str)) == 0) {
            return decodeFile;
        }
        Matrix matrix = new Matrix();
        matrix.postRotate(imageDegree);
        return Bitmap.createBitmap(decodeFile, 0, 0, decodeFile.getWidth(), decodeFile.getHeight(), matrix, true);
    }
}